﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataContexts.Supplier;

public partial class AddEditCountry : System.Web.UI.Page
{
    SupplierDataContext supplierContext = new SupplierDataContext();

    public int CountryId
    {
        get
        {
            return Helper.QSIntValue("id");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ((BaseMasterPage)this.Master).CurrentPage = PageConfig.Pages.AdminCountryAddEdit;
        if (!((BaseMasterPage)this.Master).AuthorizeCurrentUser())
            return;
        if (!IsPostBack)
        {
            if (CountryId > 0)
            {
                Country country = supplierContext.Countries.SingleOrDefault(obj => obj.CountryId == CountryId);
                //CONTROL ASSIGNMENT
                txtCountryName.Text = country.CountryName;
                txtISOCode.Text = country.ISOCode;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Country country = new Country();
        if (CountryId > 0)
            country= supplierContext.Countries.SingleOrDefault(obj => obj.CountryId==  CountryId);

        //PROPERTY ASSIGNMENT
        country.CountryName= txtCountryName.Text;
        country.ISOCode= txtISOCode.Text;
        
        if (CountryId== 0)
            supplierContext.Countries.InsertOnSubmit(country);

        supplierContext.SubmitChanges();

        Response.Redirect("ViewCountries.aspx");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewCountries.aspx");
    }
}
